package io.plus.common.packet.metadata;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

@Data
public class RequestMessage {

    /**
     * 消息创建时间
     * new Date().getTime()
     */
    protected Long createTime;
    /**
     * 消息id，全局唯一
     */
    protected String id;
    /**
     * 消息cmd命令码
     */
    protected Integer cmd;


    public byte[] toByte() {
        return null;
    }

    @Data
    public abstract static class Builder<T extends RequestMessage, B extends Builder<T, B>> {
        /**
         * 消息创建时间
         */
        protected Long createTime;
        /**
         * 消息id，全局唯一
         */
        protected String id;
        /**
         * 消息cmd命令;
         */
        protected Integer cmd;
        /**
         * 扩展字段;
         */
        protected JSONObject extras;
        private B theBuilder = this.getThis();

        public B addExtra(String key, Object value) {
            if (null == value) {
                return theBuilder;
            } else {
                if (null == extras) {
                    this.extras = new JSONObject();
                }
                this.extras.put(key, value);
                return theBuilder;
            }
        }

        /**
         * 供子类实现的抽象构建对象
         *
         * @return
         */
        public abstract T build();

        /**
         * 供子类获取自身builder抽象类;
         *
         * @return
         */
        protected abstract B getThis();


    }

}
